<template>
    <div>
        <!--面包屑导航区域-->
        <el-breadcrumb separator-class="el-icon-arrow-right">
            <el-breadcrumb-item :to="{ path: '/home_HR' }">首页</el-breadcrumb-item>
            <el-breadcrumb-item>我的岗位</el-breadcrumb-item>
        </el-breadcrumb>
        <!--卡片视图区域-->
        <el-card>
            <!--搜索与添加区域-->
            <el-row :gutter="20">
                <el-col :span="9">
                    <el-input placeholder="Please input" v-model="queryInfo.query" clearable @clear="getpos_HRList">
                        <template #append>
                            <el-button type="primary" @click="getpos_HRList">
                                <el-icon style="vertical-align: middle;">
                                    <search />
                                </el-icon>
                                <span style="vertical-align: middle;"> Search </span>
                            </el-button>
                        </template>
                    </el-input>
                </el-col>
                <el-col :span="4">
                    <el-button type="primary" @click="addPositionVisible = true">新增岗位</el-button>
                </el-col>
            </el-row>
            <!--岗位列表区域-->
            <el-table :data="poslist" :border="true" :stripe="true">
                <el-table-column prop="posID" label="岗位编号"></el-table-column>
                <el-table-column prop="name" label="岗位名称"></el-table-column>
                <el-table-column prop="type" label="岗位类别"></el-table-column>
                <el-table-column prop="company" label="所属公司"></el-table-column>
                <el-table-column prop="salary" label="最低薪资"></el-table-column>
                <el-table-column prop="status" label="审核状态"></el-table-column>
                <el-table-column label="岗位详情">
                    <template v-slot="scope">
                        <el-button type="success" @click="showDialog(scope.row.posID)">查看详情</el-button>
                    </template>
                </el-table-column>
                <el-table-column label="操作">
                    <template v-slot="scope">
                        <el-tooltip effect="dark" content="编辑" placement="top" :enterable="false">
                            <el-button type="warning" @click="showEditDialog(scope.row.posID)">
                                <el-icon style="vertical-align: middle;">
                                    <edit />
                                </el-icon>
                            </el-button>
                        </el-tooltip>
                        <el-tooltip effect="dark" content="删除" placement="top" :enterable="false">
                            <el-button type="danger" @click="removePosById(scope.row.posID)">
                                <el-icon style="vertical-align: middle;">
                                    <delete />
                                </el-icon>
                            </el-button>
                        </el-tooltip>
                    </template>
                </el-table-column>
            </el-table>
            <!--分页区域-->
            <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
                :current-page="queryInfo.pagenum" :page-sizes="[5, 10, 15, 20]" :page-size="queryInfo.pagesize"
                layout="total, sizes, prev, pager, next, jumper" :total="total">
            </el-pagination>
        </el-card>
        <!--查看详情对话框-->
        <el-dialog title="岗位详情" v-model="Visible" width="50%" @close="DialogClosed">
            <!--内容主体区域-->
            <el-descriptions class="margin-top" title="详情信息" :column="3" :size="size" border>
                <el-descriptions-item>
                    <template #label>
                        <div class="cell-item">
                            岗位编号
                        </div>
                    </template>
                    {{this.detail.posID}}
                </el-descriptions-item>
                <el-descriptions-item>
                    <template #label>
                        <div class="cell-item">
                            岗位名称
                        </div>
                    </template>
                    {{this.detail.posName}}
                </el-descriptions-item>
                <el-descriptions-item>
                    <template #label>
                        <div class="cell-item">
                            岗位大类
                        </div>
                    </template>
                    {{this.detail.bigType}}
                </el-descriptions-item>
                <el-descriptions-item>
                    <template #label>
                        <div class="cell-item">
                            岗位小类
                        </div>
                    </template>
                    {{this.detail.smallType}}
                </el-descriptions-item>
                <el-descriptions-item>
                    <template #label>
                        <div class="cell-item">
                            最低薪资
                        </div>
                    </template>
                    {{this.detail.salary}}
                </el-descriptions-item>
                <el-descriptions-item>
                    <template #label>
                        <div class="cell-item">
                            实习岗位
                        </div>
                    </template>
                    {{this.detail.intern}}
                </el-descriptions-item>
                <el-descriptions-item>
                    <template #label>
                        <div class="cell-item">
                            工作地点
                        </div>
                    </template>
                    {{this.detail.site}}
                </el-descriptions-item>
                <el-descriptions-item>
                    <template #label>
                        <div class="cell-item">
                            所属公司
                        </div>
                    </template>
                    {{this.detail.comp}}
                </el-descriptions-item>
                <el-descriptions-item>
                    <template #label>
                        <div class="cell-item">
                            负责HR
                        </div>
                    </template>
                    {{this.detail.posHR}}
                </el-descriptions-item>
                <el-descriptions-item>
                    <template #label>
                        <div class="cell-item">
                            岗位要求
                        </div>
                    </template>
                    {{this.detail.request}}
                </el-descriptions-item>
                <el-descriptions-item>
                    <template #label>
                        <div class="cell-item">
                            岗位职责
                        </div>
                    </template>
                    {{this.detail.response}}
                </el-descriptions-item>
                <el-descriptions-item>
                    <template #label>
                        <div class="cell-item">
                            审核状态
                        </div>
                    </template>
                    {{this.detail.flag}}
                </el-descriptions-item>
            </el-descriptions>
            <!--底部区域-->
            <span slot="footer" class="dialog-footer">
                <el-button @click="Visible = false" class="btns">关 闭</el-button>
            </span>
        </el-dialog>
        <!--新增岗位对话框-->
        <el-dialog title="新增岗位" v-model="addPositionVisible" width="50%" @close="addDialogClosed">
            <!--内容主体区域-->
            <el-form :model="addForm" :rules="addFormRules" ref="addFormRef" label-width="80px">
                <el-form-item label="岗位名称" prop="posName">
                    <el-input v-model="addForm.posName"></el-input>
                </el-form-item>
                <!--需要改为下拉框选择-->

                <el-form-item label="岗位类别" prop="bigType">
                    <el-cascader v-model="addForm.bigType" :options="bigOptions">
                    </el-cascader>
                </el-form-item>

                <el-form-item label="最低薪资" prop="salary">
                    <el-input v-model="addForm.salary"></el-input>
                </el-form-item>


                <el-form-item label="实习岗位" prop="intern">
                    <el-select v-model="addForm.intern" placeholder="请选择">
                        <el-option v-for="item in internOptions" :key="item.value" :label="item.label"
                            :value="item.value">
                        </el-option>
                    </el-select>
                </el-form-item>

                <el-form-item label="城市" prop="site">
                    <el-input v-model="addForm.site"></el-input>
                </el-form-item>
                <el-form-item label="岗位要求" prop="request">
                    <el-input v-model="addForm.request"></el-input>
                </el-form-item>
                <el-form-item label="岗位职责" prop="response">
                    <el-input v-model="addForm.response"></el-input>
                </el-form-item>
            </el-form>
            <!--底部区域-->
            <span slot="footer" class="dialog-footer">
                <el-button @click="addPositionVisible = false">取 消</el-button>
                <el-button type="primary" @click="addpos">确 定</el-button>
            </span>
        </el-dialog>
        <!--修改岗位对话框-->
        <el-dialog title="修改岗位" v-model="editPositionVisible" width="50%" @close="editDialogClosed">
            <!--内容主体区域-->
            <el-form :model="editForm" :rules="editFormRules" ref="editFormRef" label-width="80px">
                <el-form-item label="岗位编号" prop="posID">
                    <el-input v-model="editForm.posID" disabled></el-input>
                </el-form-item>
                <el-form-item label="岗位名称" prop="posName">
                    <el-input v-model="editForm.posName"></el-input>
                </el-form-item>
                <!--需要改为下拉框选择-->
                <el-form-item label="岗位类别" prop="bigType">
                    <el-cascader v-model="editForm.bigType" :options="bigOptions">
                    </el-cascader>
                </el-form-item>
                <el-form-item label="最低薪资" prop="salary">
                    <el-input v-model="editForm.salary"></el-input>
                </el-form-item>
                <el-form-item label="实习岗位" prop="intern">
                    <el-select v-model="editForm.intern" placeholder="请选择">
                        <el-option v-for="item in internOptions" :key="item.value" :label="item.label"
                            :value="item.value">
                        </el-option>
                    </el-select>
                </el-form-item>

                <el-form-item label="城市" prop="site">
                    <el-input v-model="editForm.site"></el-input>
                </el-form-item>
                <el-form-item label="岗位要求" prop="request">
                    <el-input v-model="editForm.request"></el-input>
                </el-form-item>
                <el-form-item label="岗位职责" prop="response">
                    <el-input v-model="editForm.response"></el-input>
                </el-form-item>
            </el-form>
            <!--底部区域-->
            <span slot="footer" class="dialog-footer">
                <el-button @click="editPositionVisible = false">取 消</el-button>
                <el-button type="primary" @click="editpos">确 定</el-button>
            </span>
        </el-dialog>
    </div>
</template>

<script>
export default {
    data() {
        return {
            queryInfo: {
                query: '',
                pagenum: 1,
                pagesize: 10
            },
            poslist: [

            ],
            total: 0,
            //查看岗位详情
            detail: {
                posID: '',
                posName: '',
                bigType: '',
                smallType: '',
                salary: 0,
                intern: 0,
                site: '',
                comp: '',
                posHR: '',
                request: '',
                response: '',
                flag: 0
            },
            // 控制查看详情对话框的显示与隐藏
            Visible: false,
            //控制新增岗位对话框的显示与隐藏
            addPositionVisible: false,
            //控制修改岗位对话框的显示与隐藏
            editPositionVisible: false,
            //新增岗位的表单数据
            addForm: {
                posName: '',
                bigType: '',
                smallType: '',
                salary: '',
                intern: '',
                site: '',
                comp: '',
                posHR: '',
                request: '',
                response: ''
            },
            editForm: {
                posID: '',
                posName: '',
                bigType: '',
                smallType: '',
                salary: '',
                intern: '',
                site: '',
                comp: '',
                posHR: '',
                request: '',
                response: ''
            },
            bigOptions: [
                {
                    value: 'IT',
                    label: 'IT',
                    children: [
                        {
                            value: '前端',
                            label: '前端'
                        },
                        {
                            value: '后端',
                            label: '后端'
                        },
                        {
                            value: '深度学习',
                            label: '深度学习'
                        },
                    ]
                },
                {
                    value: '金融',
                    label: '金融',
                    children: [
                        {
                            value: '数据分析',
                            label: '数据分析'
                        },
                        {
                            value: '精算',
                            label: '精算'
                        },
                    ]
                }
            ],
            internOptions: [
                { value: 0, label: '否' },
                { value: 1, label: '是' }
            ],
            //添加表单的验证规则
            addFormRules: {
                posname: [
                    { required: true, message: '请输入岗位名称', trigger: 'blur' }
                ],
                postype: [
                    { required: true, message: '请选择岗位类别', trigger: 'blur' }
                ],
                poscompany: [
                    { required: true, message: '请选择所属公司', trigger: 'blur' }
                ],
                possalary: [
                    { required: true, message: '请输入最低薪资', trigger: 'blur' }
                ],
                posinfo: [
                    { required: true, message: '请输入岗位详情', trigger: 'blur' }
                ]
            },
            //修改表单的验证规则
            editFormRules: {
                posname: [
                    { required: true, message: '请输入岗位名称', trigger: 'blur' }
                ],
                postype: [
                    { required: true, message: '请选择岗位类别', trigger: 'blur' }
                ],
                poscompany: [
                    { required: true, message: '请选择所属公司', trigger: 'blur' }
                ],
                possalary: [
                    { required: true, message: '请输入最低薪资', trigger: 'blur' }
                ],
                posinfo: [
                    { required: true, message: '请输入岗位详情', trigger: 'blur' }
                ]
            }
        }
    },
    created() {
        this.getpos_HRList();
    },
    methods: {
        async getpos_HRList() {
            const { data: res } = await this.$http.post('getPositionList',this.queryInfo)
            if (res.error_code !== 0) {
                return this.$message.error('获取岗位信息失败！')
            }
            this.poslist = []
            this.total = res.pageTotal;
            for (var i = 0; i < this.total; i++) {
                if (res.data.PositionList[i].posHR !== window.sessionStorage.getItem('token')) {
                    continue;
                }
                this.poslist.push({
                    posID: res.data.PositionList[i].posID,
                    name: res.data.PositionList[i].posName,
                    type: res.data.PositionList[i].smallType,
                    company: res.data.PositionList[i].comp,
                    salary: res.data.PositionList[i].salary,
                    status: res.data.PositionList[i].flag === 1 ? '通过' : '未审核'
                });
            }
        },
        //监听pagesize 改变的事件
        handleSizeChange(newSize) {
            this.queryInfo.pagesize = newSize
            this.getpos_HRList();
        },
        //监听页码值改变的事件
        handleCurrentChange(newPage) {
            this.queryInfo.pagenum = newPage
            this.getpos_HRList();
        },
        //监听查看详情对话框的关闭事件
        DialogClosed() {
            this.Visible = false
        },
        //展示查看详情对话框
        async showDialog(id) {
            const json = {
                posID: id
            };
            const { data: res } = await this.$http.post('getPositionByID', json)
            if (res.error_code !== 0) {
                return this.$message.error('查看详情失败！')
            }
            console.log(res)
            this.detail = res.data
            this.detail.intern = res.data.intern === 0 ? '否' : '是';
            this.detail.flag = res.data.flag === 0 ? '未审核' : '通过';
            this.Visible = true
        },
        //监听新增岗位对话框的关闭事件
        addDialogClosed() {
            this.$refs.addFormRef.resetFields()
        },
        //监听修改岗位对话框的关闭事件
        editDialogClosed() {
            this.$refs.editFormRef.resetFields()
        },
        //点击确定新增岗位
        addpos() {
            this.$refs.addFormRef.validate(async valid => {
                if (!valid) return
                //可以发起新增岗位的网络请求
                this.addForm.posHR = window.sessionStorage.getItem('token');
                console.log(this.addForm.bigType);
                this.addForm.bigType = this.addForm.bigType[0];
                this.addForm.smallType = this.addForm.bigType[1];
                const { data: res } = await this.$http.post('forwardPosition', this.addForm)
                if (res.error_code !== 0) {
                    return this.$message.error('新增岗位失败')
                }

                this.$message.success('新增岗位成功，请等待审核')
                //隐藏新增岗位对话框
                this.addPositionVisible = false
                //重新获取岗位数据
                this.getpos_HRList()
            })
        },
        //点击修改岗位
        editpos() {
            this.$refs.editFormRef.validate(async valid => {
                if (!valid) return
                this.editForm.posHR = window.sessionStorage.getItem('token');
                this.editForm.bigType = this.editForm.bigType[0];
                this.editForm.smallType = this.editForm.bigType[1];
                const { data: res } = await this.$http.post('changePosition', this.editForm)
                console.log(res);
                console.log(res.error_code);
                if (res.result !== "true") {
                    return this.$message.error('修改岗位失败')
                }

                this.$message.success('修改岗位成功，请等待审核')
                //隐藏新增岗位对话框
                this.editPositionVisible = false
                //重新获取岗位数据
                this.getpos_HRList()
            })
        },
        //展示岗位编辑对话框
        async showEditDialog(id) {
            const json = {
                posID: id
            };
            const { data: res } = await this.$http.post('getPositionByID', json)
            if (res.error_code !== 0) {
                return this.$message.error('查询岗位信息失败！')
            }
            this.editForm = res.data
            this.editPositionVisible = true
        },
        //根据岗位编号删除岗位
        async removePosById(id) {
            const confirmResult = await this.$confirm('此操作将永久删除该岗位, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).catch(err => err)
            if (confirmResult !== 'confirm') {
                return this.$message.info('已取消删除')
            }
            const json = {
                posID: id
            };
            const { data: res } = await this.$http.post('deletePosition', json)
            if (res.result !== "true") {
                return this.$message.error('删除岗位信息失败！')
            }
            this.$message.success('删除岗位成功！')
            this.getpos_HRList()
        }
    }
}
</script>

<style lang="less" scoped>
.btns {
    margin-top: 15px;
}
</style>